<template>
  <div class="app-container">
    <el-row>
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :inline="true" v-show="showSearch" label-width="120px" :label-position="isPhone ? 'top' : 'left'">
          <el-form-item label="学生姓名" prop="studentName">
            <el-input
              v-model="queryParams.studentName"
              placeholder="请输入学生姓名"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="高校导师姓名" prop="schoolTeacherName">
            <el-input
              v-model="queryParams.schoolTeacherName"
              placeholder="请输入高校导师姓名"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="高校导师单位" prop="schoolTeacherDept">
            <el-input
              v-model="queryParams.schoolTeacherDept"
              placeholder="请输入高校导师单位"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              @click="() => router.push('/university-mentor/edit/new?mode=add&id=new')"
              v-hasRole="['admin', 'student']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="Download"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="handleGetList"
            :columns="columns"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="list"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="学生姓名"
            align="center"
            key="nickName"
            prop="nickName"
            v-if="columns[1].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="学生账号"
            align="center"
            key="userName"
            prop="userName"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="高校导师姓名"
            align="center"
            key="schoolTeacherName"
            prop="schoolTeacherUser.nickName"
            v-if="columns[3].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="高校导师单位"
            align="center"
            key="schoolTeacherDept"
            prop="schoolTeacherUser.deptName"
            v-if="columns[4].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="高校导师职务"
            align="center"
            key="schoolTeacherPosition"
            prop="schoolTeacherUser.position"
            v-if="columns[5].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="高校导师职称"
            align="center"
            key="schoolTeacherRank"
            prop="schoolTeacherUser.rank"
            v-if="columns[6].visible"
            :show-overflow-tooltip="true"
          >
            <template #default="scope">
              {{ selectDictLabel(professional_title, scope.row.schoolTeacherUser?.rank) }}
            </template>
          </el-table-column>
          <el-table-column
            label="高校导师联系电话"
            align="center"
            key="schoolTeacherPhone"
            prop="schoolTeacherUser.phonenumber"
            v-if="columns[7].visible"
            width="140"
          />
          <el-table-column
            label="操作"
            align="center"
            width="150"
            fixed="right"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-tooltip content="详情" placement="top">
                <el-button
                  link
                  type="info"
                  icon="View"
                  @click="() => router.push(`/university-mentor/edit/${scope.row.id}?mode=detail&id=${scope.row.id}`)"
                ></el-button>
              </el-tooltip>
              <el-tooltip :content="scope.row.approvalStatus === 'approved' ? '草稿' : '修改'" placement="top">
                <el-button
                  link
                  type="primary"
                  :icon="scope.row.approvalStatus === 'approved' ? 'Document' : 'Edit'"
                  @click="() => router.push(`/university-mentor/edit/${scope.row.id}?mode=edit&id=${scope.row.id}`)"
                  v-hasRole="['admin', 'student']"
                ></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  v-if="scope.row.approvalStatus !== 'approved'"
                  v-hasRole="['admin', 'student']"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                content="提交"
                placement="top"
                v-if="
                  scope.row.approvalStatus === 'no_submit' ||
                  scope.row.approvalStatus === 'rejected'
                "
              >
                <el-button
                  link
                  type="warning"
                  icon="Upload"
                  @click="handleSubmit(scope.row)"
                  v-hasRole="['admin', 'student']"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                content="审批"
                placement="top"
                v-if="scope.row.approvalAuth"
              >
                <el-button
                  link
                  type="success"
                  icon="Check"
                  @click="() => router.push(`/university-mentor/edit/${scope.row.id}?mode=approve&id=${scope.row.id}`)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleGetList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="UniversityMentor">
import {
  getList,
  delItem,
} from "@/api/university-mentor";
import { submitApproval } from "@/api/approval";
import { selectDictLabel } from "@/utils/ruoyi";
import { isPhone } from "@/utils/index";

const router = useRouter();
const { proxy } = getCurrentInstance();
const { professional_title } = proxy.useDict("professional_title");

const list = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

// 列显隐信息
const columns = ref([
  { key: 0, label: `编号`, visible: true },
  { key: 2, label: `姓名`, visible: true },
  { key: 1, label: `账号`, visible: true },
  { key: 4, label: `高校导师姓名`, visible: true },
  { key: 5, label: `高校导师单位`, visible: true },
  { key: 6, label: `高校导师职务`, visible: true },
  { key: 7, label: `高校导师职称`, visible: true },
  { key: 8, label: `高校导师联系电话`, visible: true },
]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  studentName: undefined,
  schoolTeacherName: undefined,
  schoolTeacherDept: undefined,
});

/** 查询用户列表 */
function handleGetList() {
  loading.value = true;
  getList(queryParams).then((res) => {
    loading.value = false;
    list.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  handleGetList();
}

function resetQuery() {
  queryParams.studentName = undefined;
  queryParams.schoolTeacherName = undefined;
  queryParams.schoolTeacherDept = undefined;
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除编号为"' + id + '"的数据项？')
    .then(function () {
      return delItem(id);
    })
    .then(() => {
      handleGetList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/user/export",
    {
      ...queryParams,
    },
    `user_${new Date().getTime()}.xlsx`
  );
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 提交审核
function handleSubmit(row) {
  const id = row.id || ids.value;
  const item = list.value.find(item => item.id === id);
  proxy.$modal
    .confirm("是否确认提交审核？")
    .then(function () {
      return submitApproval({
        businessId: id,
        approvalTypeCode: "student_school_teacher_flow",
        approvalInfo: JSON.stringify(item)
      });
    })
    .then(() => {
      handleGetList();
      proxy.$modal.msgSuccess("提交成功");
    })
    .catch(() => {});
}

handleGetList();
</script>
